//
//  News.m
//  Project1
//
//  Created by 黄珂耀 on 15/7/17.
//  Copyright (c) 2015年 黄珂耀. All rights reserved.
//

#import "News.h"
#import "common.h"
#import "NewsCell.h"
#import "NewsModal.h"
#import "UIImageView+WebCache.h"
#import "jsonAnalysis.h"
#import "UIViewExt.h"
#import "NewsImageListViewController.h"

@interface News ()
{
    UITableView *_newsTableView;
    
    NSMutableArray *_newsModalArray;
    
    UIImageView *_imageView;
    
    UIImageView *_backImageView;
    
    UILabel *headerTitle;
    
    UILabel *_headerTitle;
    
    BOOL isHidden;
}

@end

@implementation News

#pragma mark - DefaultOperation

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.title = @"新闻";
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self _loadData];
    [self _createSubviews];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - LoadData

- (void)_loadData
{
    NSArray *rootArray = [jsonAnalysis initWithJsonFileName:@"news_list.json"];
    
    _newsModalArray = [[NSMutableArray alloc] init];
    
    for (NSDictionary *dic in rootArray)
    {
        NewsModal *modal = [[NewsModal alloc] init];
        
//        modal.title = [dic objectForKey:@"title"];
//        modal.type = [dic objectForKey:@"type"];
//        modal.summary = [dic objectForKey:@"summary"];
//        modal.image = [dic objectForKey:@"image"];
        
        [modal setValuesForKeysWithDictionary:dic];
        
        [_newsModalArray addObject:modal];
    }
    
}

#pragma mark - CreateSubviews

- (void)_createSubviews
{
    _newsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
    [_newsTableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]]];
    _newsTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _newsTableView.delegate = self;
    _newsTableView.dataSource = self;
    
    [self.view addSubview:_newsTableView];

    self.navigationController.delegate = self;
    
    [self _createHeaderView];
    
    _newsTableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);        //方法2 需要
    
    UINib *nib = [UINib nibWithNibName:@"NewsCell" bundle:[NSBundle mainBundle]];
    
    [_newsTableView registerNib:nib forCellReuseIdentifier:@"newsCell"];
    
}

//#pragma mark - CreateHeaderView 1
//
//- (void)_createHeaderView
//{
//    NewsModal *headerModal = _newsModalArray[0];
//    
//    _backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, KWidth, 200)];
//    NSString *headerImageURL = headerModal.image;
//    [_backImageView sd_setImageWithURL:[NSURL URLWithString:headerImageURL]];
//    
//    [self.view addSubview:_backImageView];
//    
//    
//    UIView *clearView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
//    [clearView setBackgroundColor:[UIColor clearColor]];
//
//    [_newsTableView setTableHeaderView:clearView];
//
//    _headerTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, KWidth, 40)];
//    _headerTitle.bottom = _backImageView.bottom;
//    _headerTitle.text = [NSString stringWithFormat:@"%@",headerModal.title];
//    [_headerTitle setTextAlignment:NSTextAlignmentCenter];
//    _headerTitle.font = [UIFont systemFontOfSize:18];
//    _headerTitle.textColor = [UIColor whiteColor];
//
//    [self.view addSubview:_headerTitle];
//    
//    
//}
//
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    CGFloat offY = scrollView.contentOffset.y;
//    
//    if (offY > -64)                //up
//    {
//        _backImageView.frame = CGRectMake(0, -offY, KWidth, 200);
//        _headerTitle.bottom = _backImageView.bottom;
//
//    }
//    else                           //down
//    {
//        CGFloat backHeight = - 64 - offY + 200;
//        CGFloat backWidth = KWidth / 200 * backHeight;
//        
//        _backImageView.frame = CGRectMake((KWidth - backWidth) / 2 , 64, backWidth, backHeight);
//        _headerTitle.bottom = _backImageView.bottom;
//    }
//}

#pragma mark - CreateHeaderView 2

- (void)_createHeaderView
{
    
    NewsModal *headerModal = _newsModalArray[0];
    
    headerTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, KWidth, 40)];

    headerTitle.text = [NSString stringWithFormat:@"%@",headerModal.title];
    [headerTitle setTextAlignment:NSTextAlignmentCenter];
    headerTitle.font = [UIFont systemFontOfSize:18];
    headerTitle.textColor = [UIColor whiteColor];
    
    headerTitle.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;//自动布局，自适应顶部

    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200, KWidth, 200)];
    
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    
    NSString *headerImageURL = headerModal.image;
    [_imageView sd_setImageWithURL:[NSURL URLWithString:headerImageURL]];
    
    [_newsTableView addSubview:_imageView];
    
    [_imageView addSubview:headerTitle];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat y = scrollView.contentOffset.y + 64;//根据实际选择加不加上NavigationBarHight（44、64 或者没有导航条）
    if (y < -200) {
        CGRect frame = _imageView.frame;
        frame.origin.y = y;
        frame.size.height =  -y;//contentMode = UIViewContentModeScaleAspectFill时，高度改变宽度也跟着改变
        _imageView.frame = frame;
    }
    
}

#pragma mark - NewsTableViewDelegate

//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"showTabBar" object:nil];
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsImageListViewController *newsImageListViewController = [[NewsImageListViewController alloc] init];
    
    [self.navigationController pushViewController:newsImageListViewController animated:YES];
    
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    CGRect frame = self.tabBarController.tabBar.frame;
    
    if (navigationController.viewControllers.count == 1)
    {
        frame.origin.x = 0;
    }
    else
    {
        frame.origin.x = -KWidth;
    }
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.1];
    self.tabBarController.tabBar.frame = frame;
    [UIView commitAnimations];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_newsModalArray count] - 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newsCell" forIndexPath:indexPath];
    
    cell.modal = _newsModalArray[indexPath.row + 1];
    
    return cell;
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
